<?php
class CityAction extends CommonAction{
	public function data(){
		$cityObj = D('City');
		$method = $_POST;
		switch($method['do']){
			case 'p':
				if($method['p'] == 'menu')
				{
					$map['pid'] = 0;
					foreach($cityObj->where($map)->select() as $v){
						$v['text'] = $v['city'];
						$city[] = $v;
					}
				}
				else
				{
					$map['pid'] = $method['p'];
					foreach($cityObj->where($map)->select() as $v){
						$v['leaf'] = true;
						$v['text'] = $v['city'];
						$city[] = $v;
					}
				}
				echo json_encode($city);
			break;
			case 'city':
				$map['pid'] = 0;
				$city = $cityObj->where($map)->field('id,city as name')->select();
				$city[] = array('id'=>0 , 'name'=>'无上级组');
				echo json_encode($city);
			break;
			case 'cityid':
				$map['id'] = $_POST['where'];
				$city = $cityObj->where($map)->find();
				echo json_encode($city);
			break;
			case 'usercity':
				$city = getcity();
				$map['pid'] = $city['cityid'];
				$city = $cityObj->where($map)->field('id,city as name')->select();
				$city[] = array(id=>0,'name'=>'其他城市');
				echo json_encode($city);
			break;
		}
	}
	public function index(){
		$this->display();
	}
	public function add(){
		$cityObj = D('City');
		if($this->isPost()){
			$method = $_POST;
			$data['pid'] = intval($method['pid']);
			$data['city'] = $method['city'];
			$data['sort'] = $method['sort'];
			if($cityObj->add($data))
			{
				$this->success('添加成功');
			}
			else
			{
				$this->error('添加失败');
			}
		}
	}
	public function edit(){
		$cityObj = D('City');
		if($this->isPost()){
			$method = $_POST;
			$cityObj->pid = intval($method['pid']);
			$cityObj->city = $method['city'];
			$data['sort'] = $method['sort'];
			$cityObj->id = intval($method['id']);
			if($cityObj->save())
			{
				$this->success('修改成功');
			}
			else
			{
				$this->error('修改失败');
			}
		}
	}
	public function delete(){
		if($this->isPost()){
			$method = $_POST;
			$cityObj = D('City');
			$map['id'] = intval($method['id']);
			if($cityObj->where($map)->delete()){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}

		}

	}

}